package cn.tedu.boot33.controller;

import cn.tedu.boot33.mapper.ItemMapper;
import cn.tedu.boot33.vo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {

    @Autowired(required = false)
    ItemMapper mapper;

    @RequestMapping("/insert")
    public String insert(Item item){
        mapper.insert(item);
        return "添加完成";
    }
    @RequestMapping("/select")
    public String select(){
        List <Item> list = mapper.select();
        String html="";
        for (Item i:list
             ) {
            html+="<div>";
            html+="<h3>"+i.getName()+"</h3>";
            html+="<p>"+i.getNum()+"</p>";
            html+="<p>"+i.getPrice()+"</p>";
            html+="<p>"+i.getIntro()+"</p>";
            html+="<a href='/delete?id="+i.getId()+"'>删除</a>";
            html+="</div>";
        }
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删除完成";
    }
    @RequestMapping("/update")
    public String update(Item item){
        mapper.update(item);
        return "修改完成";
    }
}
